package date;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author LIBOFEI
 * @ClassName LastSevenDay
 * @description
 * @date 2021/4/9 14:07
 * @Version 1.0
 **/
public class LastSevenDay {


    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
//        cal.set(Calendar.YEAR,2019);
//        cal.set(Calendar.MONTH,3-1);
        //当前月
        cal.setTime(new Date());
        int month = cal.get(Calendar.MONTH) + 1;
        //当前日
        int day = cal.get(Calendar.DAY_OF_MONTH);
        //获取本月最小最大天数
        int minDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, +1);
        for(int i = minDay ; i <=maxDay ; i++){
            cal.set(Calendar.DAY_OF_MONTH,i);
//            System.out.println(sf.format(new Date(cal.getTimeInMillis())));
        }
//        System.out.println("month:" + month + ",day:" + day);
//        System.out.println("maxDay:" + maxDay + ",minDay:" + minDay);

        getSevenDay();
    }



    private static Date getDateAdd(int days){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c = Calendar.getInstance();

        c.add(Calendar.DAY_OF_MONTH, -days);

        return c.getTime();

    }


    private static void getSevenDay(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        date = calendar.getTime();
        System.out.println(sdf.format(date));
        for(int i = 0 ; i < 6 ; i++){
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            date = calendar.getTime();
            System.out.println(sdf.format(date));
        }
    }
}
